package com.play.ballen.im;

import android.content.Context;
import android.content.Intent;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMUserInfo;
import com.hyphenate.easeui.constants.EaseConstant;
import com.mw.plp.uim.common.utils.ToastUtils;
import com.play.ballen.R;
import com.play.ballen.databinding.ActivityChatSettingBinding;
import com.yuven.baselib.component.BaseMVVMActivity;
import com.yuven.baselib.component.viewmodel.BaseViewModel;

import java.util.List;
import java.util.Map;

public class ChatSettingActivity extends BaseMVVMActivity<BaseViewModel, ActivityChatSettingBinding> {

    private String account;

    public static void actionStart(Context context, String conversationId) {
        Intent intent = new Intent(context, ChatSettingActivity.class);
        intent.putExtra(EaseConstant.EXTRA_CONVERSATION_ID, conversationId);
        context.startActivity(intent);
    }

    @Override
    protected void initIntent(Intent intent) {
        account = intent.getStringExtra(EaseConstant.EXTRA_CONVERSATION_ID);
    }
    @Override
    protected int layoutId() {
        return R.layout.activity_chat_setting;
    }

    @Override
    protected Class<BaseViewModel> viewModelClass() {
        return BaseViewModel.class;
    }

    @Override
    protected void initView() {
        dataBinding.ivBtnBack.setOnClickListener(v -> onBackPressed());
        dataBinding.tvBtnClear.setOnClickListener(v -> {
            EMClient.getInstance().chatManager().deleteConversation(account,true);
        });
    }

    @Override
    protected void initData() {
        getUserInfo();
        getBlackStatus();
    }
    private void getUserInfo(){
        String[] userId = new String[]{account};
        EMClient.getInstance().userInfoManager().fetchUserInfoByUserId(userId, new EMValueCallBack<Map<String, EMUserInfo>>() {
            @Override
            public void onSuccess(Map<String, EMUserInfo> value) {
                EMUserInfo userInfo=value.get(account);
               runOnUiThread(()->{
                   setUserInfo(userInfo);
               });
            }

            @Override
            public void onError(int error, String errorMsg) {
                ToastUtils.showFailToast("用户信息查找失败");
            }
        });
    }
    public void setUserInfo(EMUserInfo userInfo){
        if (userInfo!=null){
            dataBinding.tvName.setText(userInfo.getNickname());
        }else {
            ToastUtils.showFailToast("用户信息查找失败");
        }
    }
    public void setBlackStatus(boolean isBlack){
        dataBinding.swBtn.setOnCheckedChangeListener(null);
        dataBinding.swBtn.setChecked(isBlack);
        dataBinding.swBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            updateBlackStatus(isChecked);
        });
    }

    private void getBlackStatus(){
        EMClient.getInstance().contactManager().asyncGetBlackListFromServer(new EMValueCallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> value) {
                runOnUiThread(() -> {
                    setBlackStatus(value!=null && value.contains(account));
                });
            }

            @Override
            public void onError(int error, String errorMsg) {

            }
        });
    }
    private void updateBlackStatus(boolean isBlack){
        if (isBlack){
            EMClient.getInstance().contactManager().asyncAddUserToBlackList(account, false, new EMCallBack() {
                @Override
                public void onSuccess() {
                    getBlackStatus();
                }

                @Override
                public void onError(int code, String error) {
                    getBlackStatus();
                }
            });

        }else{
            EMClient.getInstance().contactManager().asyncRemoveUserFromBlackList(account, new EMCallBack() {
                @Override
                public void onSuccess() {
                    getBlackStatus();
                }

                @Override
                public void onError(int code, String error) {
                    getBlackStatus();
                }
            });
        }


    }


}
